package com.hsy.crm.web.service.base;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.hsy.crm.web.config.CrmMapper;
import com.hsy.crm.web.domain.cm.App;
import com.hsy.crm.web.domain.cm.PageResult;
import com.hsy.crm.web.domain.entity.BaseEntity;
import com.hsy.crm.web.domain.ex.ServiceException;
import com.hsy.crm.web.utils.PropertyUtils;
import com.hsy.crm.web.utils.ServiceUtils;
import com.hsy.crm.web.utils.SqlUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/**
 * Created by admin on 2018/4/25.
 */
public class BaseService<T> {

    @Autowired
    private CrmMapper<T> crmMapper;

    /**
     * 添加一行数据
     * 自动生成id
     */
    public int insert(T bean) {
        return this.insert(bean,Boolean.FALSE);
    }

    /**
     * 添加一行数据   并判断名称是否重复
     * @return
     */
    public int insert(T bean,boolean repeat) {
        if( repeat && repeat_check_insert(bean) ){
            return 0;
        }

        ServiceUtils.init_entity_insert(bean);
        crmMapper.insert(bean);
        return 1;
    }

    /**
     * 更新
     */
    public int update(T bean) {
        return update(bean,Boolean.FALSE);
    }

    /**
     * 根据id customerId 更新数据
     * 有值更新 空值不更新
     */
    public int update(T bean,boolean repeat) {
        if( repeat && repeat_check_update(bean) ){
            return 0;
        }

        ServiceUtils.init_entity_update(bean);
        crmMapper.updateByPrimaryKeySelective(bean);
        return 1;
    }

    public PageResult<T> page(Object bean) {
        BaseEntity entity = (BaseEntity) bean;
        Page page = PageHelper.startPage(entity.getPage(),entity.getLimit());
        PageResult<T> pageResult = new PageResult();
        Example example = new Example(bean.getClass());
        example.setOrderByClause(SqlUtils.order(App.Column.CREATE_TIME, App.SqlOrder.DESC));
        pageResult.setRows(crmMapper.selectByExample(example));
        pageResult.setTotal(page.getTotal());
        return pageResult;
    }

    /**
     * 根据id customerId删除
     */
    public int delete(Object bean) {
        ServiceUtils.check(bean,App.DbOperatorType.DELETE);
        return crmMapper.deleteByPrimaryKey(bean);
    }

    public List<T> select(T bean) {
        return crmMapper.select(bean);
    }

    public T selectByName(T bean) {
        T select = null;
        try {
            select = (T) bean.getClass().newInstance();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }

        String name = PropertyUtils.getStringProperty(bean,App.Property.NAME);
        String customerId = PropertyUtils.getStringProperty(bean,App.Property.CUSTOMER_ID);

        PropertyUtils.setProperty(select,App.Property.CUSTOMER_ID,name);
        PropertyUtils.setProperty(select,App.Property.CUSTOMER_ID,customerId);
        return crmMapper.selectOne(select);
    }

    public T selectOne(T bean) {
        return crmMapper.selectOne(bean);
    }

    public List<T> selectAll() {
        return crmMapper.selectAll();
    }

    public int count(T bean) {
        return crmMapper.selectCount(bean);
    }


    public T selectByPrimaryKey(T bean) {
        return crmMapper.selectByPrimaryKey(bean);
    }



    private boolean repeat_check_insert(Object bean){
        Example example = new Example(bean.getClass());
        example.setTableName( ((BaseEntity)bean).getDynamicTableName() );
        Example.Criteria criteria = example.createCriteria();
        String name = PropertyUtils.getStringProperty(bean,App.Property.NAME);
        if( StringUtils.isEmpty(name) ){
            throw new ServiceException("bean field property name is null");
        }
        criteria.andEqualTo(App.Property.NAME,name);
        int count = crmMapper.selectCountByExample(example);

        return count > 0;
    }

    private boolean repeat_check_update(Object bean){
        Example example = new Example(bean.getClass());
        example.setTableName( ((BaseEntity)bean).getDynamicTableName() );
        Example.Criteria criteria = example.createCriteria();
        String name = PropertyUtils.getStringProperty(bean,App.Property.NAME);
        String companyId = PropertyUtils.getStringProperty(bean,App.Property.LOGIN_COMPNAY_ID);
        String id = PropertyUtils.getStringProperty(bean,App.Property.ID);
        if( StringUtils.isAnyEmpty(name,companyId,id) ){
            throw new ServiceException("bean field property name,customerId,companyId is null");
        }
        criteria.andEqualTo(App.Property.NAME,name);
        criteria.andEqualTo(App.Property.COMPNAY_ID,companyId);
        criteria.andNotEqualTo(App.Property.ID,id);
        int count = crmMapper.selectCountByExample(example);

        return count > 0;
    }


}